// 引入 Vue 和 VueRouter
import { createApp } from 'vue';
import { createRouter, createWebHashHistory, RouterView } from 'vue-router';
// 导入组件

import login from '../pages/login.vue'
import layout from '../pages/layout.vue'
import home from '../pages/system/home.vue'
import dept from '../pages/system/dept.vue'
// import user from '../pages/system/user.vue'
import EmptyLaout from '../pages/EmptyLayout.vue'
import emp from '../pages/system/emp.vue'
import role from '../pages/system/role.vue'
import permission from '../pages/system/permission.vue'
import { info } from '../api/Login';

import infoVue from '../pages/association/info.vue'
import associationEmptyVue from '../pages/AssociationEmpty.vue'
import bsAssociationCouncilListVue from '../pages/association/BsAssociationCouncilList.vue'
import bsWxUserListVue from '../pages/association/BsWxUserList.vue'
import bsApplyListVue from '../pages/association/BsApplyList.vue'
import BsMemberListVue from '../pages/association/BsMemberList.vue';
import BsNewsListVue from '../pages/association/BsNewsList.vue';

// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(), // 使用 HTML5 History 模式
  // 配置路由
  routes: [
  
    { 
      path: '/login',
      component: login,
      meta:{title:"登录页"} 
    },
    { 
      path:'/',
      component:layout,
      redirect:"/home",
      children:[
        {
          path:'home',
          component : home,
          meta:{title:"首页"}
        },
        {
          path:"system",
          name:'system',
          redirect:"/system/dept",
          // component: () => RouterView,
          component: EmptyLaout,
          meta:{title:"系统管理"},
          children:[
            {
              path:'dept',
              name:'dept',
              component:dept,
              meta:{title:"部门管理"}
            },
            {
              path:'emp',
              name:'emp',
              component: emp,
              meta:{title:"用户管理"}
            },{
              path:'role',
              name:role,
              component:role,
              meta:{title:"角色管理"}
            },{
              path:'permission',
              name:'permission',
              component:permission,
              meta:{title:'权限管理'}
            }
          ]
        },
        {
           path:"association",
          name:'association',
          redirect:"/association/info",
          // component: () => RouterView,
          component: associationEmptyVue,
          meta:{title:"协会管理"},
          children:[
            {
              path:'info',
              name:'info',
              component: infoVue,
              meta:{title:"协会信息"}
            },{
              path:'councilList',
              name:'councilList',
              component:bsAssociationCouncilListVue,
              meta:{title:"理事会成员管理"}
            },{
              path:'bsWxUserList',
              name:'bsWxUserList',
              component:bsWxUserListVue,
              meta:{title:"会员管理"}
            },{
              path:'bsApplyList',
              name:'bsApplyList',
              component:bsApplyListVue,
              meta:{title:"会员申请"}
            },{
              path:'bsMemberList',
              name:'bsMemberList',
              component:BsMemberListVue,
              meta:{title:"会员信息"}
            },{
              path:'bsNewsList',
              name:'bsNewsList',
              component:BsNewsListVue,
              meta:{title:"新闻展示"}
            }
            
          ]
        }
      ]
    },
  ]
});

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const token =  localStorage.getItem('token') // 获取 token
  const isLogin = !!token // 判断是否已登录

  if (to.path === '/login') {
    // 访问的是登录页
    if (isLogin) {
      // 已登录，跳转首页
      next('/')
    } else {
      // 未登录，直接放行
      next()
    }
  } else {
    // 访问的是非登录页
    if (isLogin) {

      // 动态导入并使用 store
      const { useUserInfoStore } = await import('../store/index')
      const userStore = useUserInfoStore()

      
      if( !(userStore.nav && userStore.nav.length > 0) ){
        let res = await info()
        // 将数据放入到pinia中
        // 保存菜单
        userStore.setNav(res.data.data.menus)
        // 保存权限集合
        userStore.setPermission(res.data.data.permissions)
     
      }
      // 已登录，放行
      next()
    } else {
      // 未登录，跳转登录页
      next('/login')
    }
  }
})

export default router;